package com.gongpingjialoan.activity.sell;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.main.BaseActivity;
import com.gongpingjialoan.activity.tool.WebViewActivity;
import com.gongpingjialoan.api.API;
import com.gongpingjialoan.bean.Monitor;
import com.gongpingjialoan.constant.Const;
import com.gongpingjialoan.data.SellPrivateCarBean;
import com.gongpingjialoan.network.NetDataJson;
import com.gongpingjialoan.utility.PhotoUtil;
import com.gongpingjialoan.utility.StepMonitor;
import com.gongpingjialoan.widget.AddMaintainView;

import org.json.JSONArray;
import org.json.JSONObject;

/**
 * Created by Administrator on 2016/8/16.
 */
public class SellPrivateStepActivity3 extends BaseActivity {

    private AddMaintainView baoyangView, chuxiangView;

    private static final int REQUEST_CROP_CAMERA_BAOYANG = 22;

    private static final int REQUEST_CROP_CAMERA_CHUXIAN = 23;


    private static final int REQUEST_CAMERA_BAOYANG = 100;

    private static final int REQUEST_CAMERA_CHUXIAN = 101;

    private SellPrivateCarBean mSellPrivateCarBean;

    private NetDataJson mNetDataJson;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isSendData = true ;
        isScrollPage = true;
        action = "sell_pubCarSource_maintainImage";
        super.onCreate(savedInstanceState);
        mSellPrivateCarBean = SellPrivateCarBean.getInstance();
        setContentView(R.layout.sell_car_step3);
        setTitle("个人卖车");
        initView();
        initNoknow();
    }


    private void initNoknow(){
        View view = findViewById(R.id.no_know);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SellPrivateStepActivity3.this, WebViewActivity.class);
                intent.putExtra("url", Const.maiche);
                intent.putExtra("content", "过户6大证件");
                startActivity(intent);
            }
        });

    }

    private void initView() {

        baoyangView = (AddMaintainView) findViewById(R.id.maintain);
        chuxiangView = (AddMaintainView) findViewById(R.id.chuxian);
        baoyangView.setTitle("最近保养记录");
        chuxiangView.setTitle("最近出险记录");
        baoyangView.setMaxNum(3);
        chuxiangView.setMaxNum(3);
        baoyangView.setId(REQUEST_CAMERA_BAOYANG);
        chuxiangView.setId(REQUEST_CAMERA_CHUXIAN);


        if(mSellPrivateCarBean.maintancesPhotoModels!=null){
            baoyangView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    baoyangView.setData(mSellPrivateCarBean.maintancesPhotoModels);
                }
            }, 200);

        }
        if(mSellPrivateCarBean.insurancePhotoModels!=null){
            chuxiangView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    chuxiangView.setData(mSellPrivateCarBean.insurancePhotoModels);
                }
            }, 200);

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            // Bundle bundle = data.getExtras();
            switch (requestCode) {
                case Const.PICK_MAINTAIN:
                    if (data.getIntExtra("id", 0) == REQUEST_CAMERA_BAOYANG) {
                        baoyangView.photoCallBack(data);
                        mSellPrivateCarBean.maintancesPhotoModels = baoyangView.photos;
                    }
                    if (data.getIntExtra("id", 0) == REQUEST_CAMERA_CHUXIAN) {
                        chuxiangView.photoCallBack(data);
                        mSellPrivateCarBean.insurancePhotoModels = chuxiangView.photos;
                    }
                    break;
                case REQUEST_CROP_CAMERA_BAOYANG:
                    baoyangView.cameraCallback();
                    mSellPrivateCarBean.maintancesPhotoModels = baoyangView.photos;
                    break;

                case REQUEST_CROP_CAMERA_CHUXIAN:
                    chuxiangView.cameraCallback();
                    mSellPrivateCarBean.insurancePhotoModels = chuxiangView.photos;
                    break;

                case REQUEST_CAMERA_BAOYANG:
                    PhotoUtil.onPhotoFromCamera(this, REQUEST_CROP_CAMERA_BAOYANG, baoyangView.camearPath, 3,
                            2, 2000);
                    break;

                case REQUEST_CAMERA_CHUXIAN:
                    PhotoUtil.onPhotoFromCamera(this, REQUEST_CROP_CAMERA_CHUXIAN, chuxiangView.camearPath, 3,
                            2, 2000);
                    break;
            }
        }
    }


    //跳过
    public void jump(View v) {
        StepMonitor.getInstance().addMonitor(new Monitor("jump", "sell_pubCarSource_maintainImage", System.currentTimeMillis(), 1f));
        mSellPrivateCarBean.maintenance_images = "[]";
        mSellPrivateCarBean.insurance_images = "[]";
        gotoSellCar();
    }


    //确认提交
    public void step3(View v) {
        JSONArray baoyangJsonArray = null, chuxiangJsonArray = null;
        try {
            baoyangJsonArray = baoyangView.getUrls();
            if (baoyangJsonArray.length() == 0) {
                Toast.makeText(this, "请上传保养图片", Toast.LENGTH_SHORT).show();
                return;
            }
            chuxiangJsonArray = chuxiangView.getUrls();
            if (chuxiangJsonArray.length() == 0) {
                Toast.makeText(this, "请上传出险图片", Toast.LENGTH_SHORT).show();
                return;
            }
        } catch (Exception e) {

        }
        mSellPrivateCarBean.maintenance_images = baoyangJsonArray.toString();
        mSellPrivateCarBean.insurance_images = chuxiangJsonArray.toString();
        gotoSellCar();
    }

    private void gotoSellCar() {
        if (mNetDataJson == null) {
            mNetDataJson = new NetDataJson(new NetDataJson.OnNetDataJsonListener() {
                @Override
                public void onDataJsonError(String errorMessage) {
                    hidenProgressDialog();
                    Toast.makeText(SellPrivateStepActivity3.this, errorMessage, Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onDataJsonUpdate(JSONObject json) {
                    hidenProgressDialog();
                    startActivity(new Intent(SellPrivateStepActivity3.this , SellPrivateSuccessActivity.class));
                }
            });
        }
        mNetDataJson.setUrl(API.sell_private_car);
        mNetDataJson.addParam("brand", mSellPrivateCarBean.brandSlug);
        mNetDataJson.addParam("model", mSellPrivateCarBean.modelSlug);
        mNetDataJson.addParam("model_detail", mSellPrivateCarBean.modelDetailSlug);
        mNetDataJson.addParam("year", mSellPrivateCarBean.year);
        mNetDataJson.addParam("month", mSellPrivateCarBean.month);
        mNetDataJson.addParam("mile", mSellPrivateCarBean.mile);
        mNetDataJson.addParam("city", mSellPrivateCarBean.city);
        mNetDataJson.addParam("price", mSellPrivateCarBean.maxPrice);
        mNetDataJson.addParam("region", mSellPrivateCarBean.address);
        mNetDataJson.addParam("phone", mSellPrivateCarBean.phone);
        mNetDataJson.addParam("contact", mSellPrivateCarBean.contract);
        mNetDataJson.addParam("images", mSellPrivateCarBean.images);
        mNetDataJson.addParam("identify_images",mSellPrivateCarBean.identify_image);
        mNetDataJson.addParam("maintenance_images", mSellPrivateCarBean.maintenance_images);
        mNetDataJson.addParam("insurance_images",mSellPrivateCarBean.insurance_images);
        mNetDataJson.request("post");
        showProgressDialog("加载中...");

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mNetDataJson != null) {
            mNetDataJson.cancelTask();
            mNetDataJson = null;
        }
        mSellPrivateCarBean.maintenance_images = "";
        mSellPrivateCarBean.insurance_images = "";
    }
}
